package com.wyz.controller;

import com.github.pagehelper.PageInfo;
import com.wyz.entity.Grades;
import com.wyz.entity.Student;
import com.wyz.service.GradesService;
import com.wyz.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

@Controller
public class StudentController {
    @Autowired
    StudentService studentService;
    @Autowired
    GradesService gradesService;
    @RequestMapping("StudentSeach")
    public String seach(String name,String flag, Model model, String grade, @RequestParam(defaultValue = "1") int indexPage, HttpSession session){
        if (name!=null) {
            session.setAttribute("name", name);
        }
        if (grade!=null){
            session.setAttribute("class",grade);
        }
        model.addAttribute("class",session.getAttribute("class"));
        model.addAttribute("name",session.getAttribute("name"));
        PageInfo<Student> pageInfo = studentService.findAll(indexPage, 5, session.getAttribute("name")==null ? "":session.getAttribute("name").toString(),session.getAttribute("class")==null ? "":session.getAttribute("class").toString());
        model.addAttribute("grades",gradesService.findAll());
        model.addAttribute("pageInfo",pageInfo);
        return "studentList";
    }
    @RequestMapping("add")
    public String add(Student student){
        System.out.println(student);
       studentService.add(student);
       return "redirect:StudentSeach";
    }
}
